const tools = require("../../utils/tool")

// components/content/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    labelList: {
      type: Boolean,
      value: false
    },
    recommend: {
      type: Array,
      observer: async function (newVal) {
        console.log(newVal,1223)
        this.setData({
          recommend1: newVal
        })
        const cloneVal = JSON.parse(JSON.stringify(newVal));
        setTimeout(() => {
          this.initObserver(cloneVal)
        }, 0);
        // this.setBiao(cloneVal)
      }
    },
    mine: {
      value: 0,
      type: Number
    },
    isAudit: {
      value: -1,
      type: Number
    },
    typ:{
      value:0,
      type:Number
    }
  },
  data: {
    height: '',
    solid: [], 
    recommend1: [] 
  },
  methods: {
    // 重置标签
    // setBiao(cloneVal) {
    //   console.log(cloneVal)
    //   for (const item of cloneVal) {
    //       item.labels = item.labels.split(',')
    //       if (item.labels[0].length > 14) {
    //         item.labels = item.labels.slice(0, 1)
    //       } else {
    //         item.labels = item.labels.slice(0, 2)
    //       }
    //   }
    //   this.setData({
    //     recommend1: cloneVal
    //   })
    // },
    initObserver(cloneVal) {
      var that = this;
      return new Promise((resolve, reject) => {
        for (let i in cloneVal) {
          that.createIntersectionObserver().relativeToViewport({
            bottom: 20
          }).observe('.item-' + i, (ret) => {
            var show = 'recommend1[' + i + '].show'
            if (ret.intersectionRatio > 0) {
              this.setData({
                [show]: true
              })
            }
          })
        }
      })
    },
    // 点赞

    // bindgive(e){
    //   console.log(e.currentTarget.dataset.id)
    //   let id = e.currentTarget.dataset.id //文章id
    //   let index = e.currentTarget.dataset.index
    //   console.log(id,index)
    //   this.triggerEvent('myevent',{id,index})
    // },

    bindGive(e) {
      console.log(e)
      let id = e.currentTarget.dataset.id //文章id
      let index = e.currentTarget.dataset.index
       let methods=this.data.recommend1[index].praised?'delete':'post'
      let give = tools.request('/aty/applet/a/praise/' + id, methods, {}, wx.getStorageSync('token'))
      give.then(res => {
          wx.hideLoading()
          console.log(res.statusCode==204)
          
          if (res.statusCode==200 || res.statusCode==204) {
            this.setData({
              [`recommend1[${index}].praised`]: !this.data.recommend1[index].praised
            })
            console.log(this.data.recommend1[index].praised)
            if (this.data.recommend1[index].praised) {
              tools.showToast('点赞成功')
            } else {
              tools.showToast('取消点赞')
            }
          } else {
            tools.showToast(res.data.message, 'error')
          }
        })
        .catch(err => {
          tools.showToast(err.errMsg, 'error')
        })
    },
    // 点击头像查看别人主页
    bindZhuYe(e) {
      let ismy = e.currentTarget.dataset.ismy
      let user_id = e.currentTarget.dataset.userid
      let isAdmin = e.currentTarget.dataset.isadmin
      if (ismy && !isAdmin) {
        wx.switchTab({
          url: '/pages/mine/mine/mine'
        })
        return
      }
      if (!ismy && !isAdmin && user_id != undefined) {
        wx.navigateTo({
          url: '/pages/mine/otherMine/otherMine?user_id=' + user_id,
        })
        return
      }
    }
  }
})